package com.zy.test.widget.fragment

import android.content.res.ColorStateList
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.FragmentTransaction
import androidx.viewpager.widget.ViewPager
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import com.zy.test.widget.R


//参考 : https://blog.csdn.net/LoveFHM/article/details/127701954?spm=1001.2014.3001.5501
class RadioGroupFragment : Fragment() {

    private lateinit var viewPager: ViewPager
    private lateinit var radioGroup: RadioGroup

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_test_radio_group, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
    }

    private lateinit var fragments: Array<Fragment>


    private fun initView(view: View) {
        initFragment()
        viewPager = view.findViewById(R.id.fragment_container_viewpager)
        radioGroup = view.findViewById(R.id.nav_radio_group)
        viewPager.adapter = object :
            FragmentPagerAdapter(parentFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(position: Int): Fragment {
                return fragments[position]
            }

            override fun getPageTitle(position: Int): CharSequence {
                return "titles[$position]"
            }
        }
        initListener()

        viewPager.currentItem = 0
        radioGroup.check(R.id.nav_radio_button_1)
    }


    private fun initListener() {

        radioGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) { //点击切换viewpager页面
                R.id.nav_radio_button_1 -> {
                    viewPager.currentItem = 0
                }
                R.id.nav_radio_button_2 -> {
                    viewPager.currentItem = 1
                }
                R.id.nav_radio_button_3 -> {
                    viewPager.currentItem = 2
                }
                R.id.nav_radio_button_4 -> {
                    viewPager.currentItem = 3
                }
            }
        }
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) { //当页面左右滑动发生改变时，radiogroup也改变子项的选择状态
                when (position) {
                    0 -> radioGroup.check(R.id.nav_radio_button_1)
                    1 -> radioGroup.check(R.id.nav_radio_button_2)
                    2 -> radioGroup.check(R.id.nav_radio_button_3)
                    3 -> radioGroup.check(R.id.nav_radio_button_4)
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
    }


    private fun initFragment() {
        fragments = arrayOf(
            TabLayoutFragment.newInstance("Android"),
            TabLayoutFragment.newInstance("算法"),
            TabLayoutFragment.newInstance("发现"),
            TabLayoutFragment.newInstance("设置")
        )
    }

    companion object {

        @JvmStatic
        fun newInstance() =
            RadioGroupFragment().apply {
                arguments = Bundle().apply {
                    putString("ARG_PARAM1", "param1")
                    putString("ARG_PARAM2", "param2")
                }
            }
    }
}